package com.life.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.HashMap;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import javax.swing.JFrame;

import com.life.constants.Constants;

public class GraphAdapter extends AbstractAdapter
{

    JFrame graphFrame = null;

    int width = Constants.GRAPH_WIDTH;

    int height = Constants.GRAPH_HEIGHT;

    String chartTitle = null;

    String domainAxisLabel = null;

    String rangeAxisLabel = null;

    public GraphAdapter()
    {
	init();
    }

    public void init()
    {
	Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

	int widthOffset = (int) ((dim.getWidth() - width) / 2);
	int heightOffset = (int) ((dim.getHeight() - height) / 2);

	graphFrame = new JFrame();
	graphFrame.setBounds(widthOffset, heightOffset, width, height);

	graphFrame.setVisible(true);
    }

    @Override
    public Object get()
    {
	// TODO Auto-generated method stub
	return null;
    }

    @Override
    public void set(Object data)
    {

	//	 create the dataset...
	final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

	HashMap<String, ArrayList> hash = (HashMap<String, ArrayList>) data;

	for (String val : hash.keySet())
	{
	    ArrayList<Integer> list = (ArrayList<Integer>) hash.get(val);

	    for (int i = 0; i < list.size(); i++)
	    {
		dataset.addValue(list.get(i).intValue(), val, new Integer(i));
	    }
	    
	}
	
	//	 create the chart...
	final JFreeChart chart = ChartFactory.createLineChart(chartTitle, // chart title
		domainAxisLabel, // domain axis label
		rangeAxisLabel, // range axis label
		dataset, // data
		PlotOrientation.VERTICAL, // orientation
		true, // include legend
		true, // tooltips
		false // urls
		);
 

	final ChartPanel chartPanel = new ChartPanel(chart);
	chartPanel.setDisplayToolTips(true);
	chartPanel.setPreferredSize(new Dimension(500, 270));
	graphFrame.setContentPane(chartPanel);
	
	graphFrame.validate();
	graphFrame.pack();
	graphFrame.repaint();
    }

    @Override
    public void setEnabled(boolean value)
    {
	// TODO Auto-generated method stub

    }

    public void setVisible(boolean value)
    {
	graphFrame.pack();
	graphFrame.setVisible(value);
    }

    /**
     * @return the chartTitle
     */
    public String getChartTitle()
    {
	return chartTitle;
    }

    /**
     * @param chartTitle the chartTitle to set
     */
    public void setChartTitle(String chartTitle)
    {
	this.chartTitle = chartTitle;
    }

    /**
     * @return the domainAxisLabel
     */
    public String getDomainAxisLabel()
    {
	return domainAxisLabel;
    }

    /**
     * @param domainAxisLabel the domainAxisLabel to set
     */
    public void setDomainAxisLabel(String domainAxisLabel)
    {
	this.domainAxisLabel = domainAxisLabel;
    }

    /**
     * @return the rangeAxisLabel
     */
    public String getRangeAxisLabel()
    {
	return rangeAxisLabel;
    }

    /**
     * @param rangeAxisLabel the rangeAxisLabel to set
     */
    public void setRangeAxisLabel(String rangeAxisLabel)
    {
	this.rangeAxisLabel = rangeAxisLabel;
    }

}
